package cn.diancan.util.images;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
 * 图片抓取类
 * @author Administrator
 *
 */
public class CatchImg {

	public void f(String inUrl){
		try {
            URL url = new URL(inUrl);
            Image src = javax.imageio.ImageIO.read(url); // 构造Image对象  
            int width = src.getWidth(null); // 得到源图宽  
            int height = src.getHeight(null); // 得到源图长  
            BufferedImage tag = new BufferedImage(width, height,  
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, width, height, null); // 绘制缩小后的图  
            //构造文件名  
            int startIndex = inUrl.lastIndexOf("/");  
            String filepath = "E:/"+inUrl.substring(startIndex+1);  
            FileOutputStream out = new FileOutputStream(filepath);
            
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
            encoder.encode(tag); // 近JPEG编码  
            // System.out.print(width+ "* "+height);  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();
        }
	}
	
	
	
	
	public BufferedImage loadImageLocal(String imgName)   {   
	    try  {   
	        return ImageIO.read(new File(imgName));
	    } catch (IOException e)   
	    {   
	        System.out.println(e.getMessage());
	    }   
	    return null;   
	}   
	  
	  
	/**  
	* 导入网络图片到缓冲区  
	*/  
	public BufferedImage loadImageUrl(String imgName)   
	{   
	    try  {   
	        URL url = new URL(imgName);   
	        return ImageIO.read(url);
	    } catch (IOException e)   {   
	        System.out.println(e.getMessage());   
	    }   
	    return null;   
	}   
	  
	public void all(String url){
		BufferedImage bImage = loadImageUrl(url);
        //构造文件名  
        int startIndex = url.lastIndexOf("/");  
        String filepath = "E:/"+url.substring(startIndex+1);  
		
		writeImageLocal(filepath ,bImage );
		
	}
	  
	  
	/**  
	* 生成新图片到本地  
	*/  
	public void writeImageLocal(String newImage, BufferedImage img)   
	{   
	    if (newImage != null && img != null)   
	    {   
	        try  
	        {   
	            File outputfile = new File(newImage);   
	            ImageIO.write(img, "jpg", outputfile); 
	        } catch (IOException e)   
	        {   
	            System.out.println(e.getMessage());   
	        }
	    }   
	}   
	
	
	public static void main(String[] args) {
	//	new CatchImg().f("http://down.tutu001.com/d/file/20110321/f16c60f12500de42fbae9c6ddc_560.jpg");
		//new CatchImg().f("http://su.bdimg.com/static/skin/img/logo_white.png");
		//new CatchImg().all("http://down.tutu001.com/d/file/20110321/f16c60f12500de42fbae9c6ddc_560.jpg");
		long starttime = System.currentTimeMillis();
		new CatchImg().img3();
		long endtime = System.currentTimeMillis();
		System.out.println(endtime-starttime);
	}
	
	
	public void img3(){
		// TODO Auto-generated method stub  
        try {  
            //图片的http全路径  
            String imgurl = "http://q.qlogo.cn/qqapp/1104574025/389F870E5B75C311E7C6B041A2CC8FA7/100";
            URL url = new URL(imgurl);  
            BufferedInputStream  in = new BufferedInputStream(url.openStream());  
            //保存的图片文件名  
            File img = new File("D:/aa/img04.jpg");
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(img));  
            byte[] buf = new byte[1];  
            int len;
            while((len = in.read(buf)) != -1) {  
                out.write(buf ,0,len);
            }
            in.close();  
            out.close();  
        } catch (MalformedURLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
	 
	
	
}
